package com.lyl.momanapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.SendCaptcha;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.bean.User;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyBaseActivity;

import java.util.HashMap;

/**
 * 名字 梁亚龙
 * 时间 2017/9/26.
 * QQ邮箱 503934353@qq.com
 */

public class UserRegisterActivity extends MyBaseActivity {


    private EditText mPWD;
    private EditText mPhone, mYanZheng;
    private Button mBut;
    private LinearLayout mButYZ;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case URL.ID_SEND_CAPTCHA: {
                    SendCaptcha sendCaptcha = new Gson().fromJson(msg.obj.toString(), SendCaptcha.class);
                    if (sendCaptcha.getState().equals("M00000")) {
                        Toast.makeText(UserRegisterActivity.this, sendCaptcha.getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(UserRegisterActivity.this, sendCaptcha.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }
                break;
                case URL.ID_USER_LOGIN: {
                    URL.Userlogin  = new Gson().fromJson(msg.obj.toString(), User.login.class);
                    if (URL.Userlogin.getState().equals("M00000")) {
                        Toast.makeText(UserRegisterActivity.this, URL.Userlogin.getMessage(), Toast.LENGTH_SHORT).show();
                        UserRegisterActivity.this.finish();
                    } else {

                        Toast.makeText(UserRegisterActivity.this, URL.Userlogin.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                }
                break;
            }
        }
    };

    @Override
    public void initUI() {
        setContentView(R.layout.activity_user_register);
        mPWD = ((EditText) findViewById(R.id.register_pwd));
        mPhone = (EditText) findViewById(R.id.register_phone);
        mBut = ((Button) findViewById(R.id.Register_login));
        mYanZheng = (EditText) findViewById(R.id.register_yanzheng);
        mButYZ = ((LinearLayout) findViewById(R.id.register_yanzheng_but));

    }

    @Override
    public void initData() {
        mBut.setOnClickListener(this);
        mButYZ.setOnClickListener(this);
    }

    @Override
    public void addListeners() {
        findViewById(R.id.fanhui).setOnClickListener(this);
        findViewById(R.id.zhuye).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Register_login: {
                //                Intent intent = new Intent(UserRegisterActivity.this, UserLoginActivity.class);
                //
                //                intent.putExtra("name", mPhone.getText().toString().trim());
                //                intent.putExtra("pwd", mPWD.getText().toString().trim());
                //                setResult(0, intent);
                //                finish();
                HashMap<String, String> map = new HashMap<>();
                map.put("phone", mPhone.getText().toString());
                map.put("access_type", "0");
                map.put("captcha", mYanZheng.getText().toString());
                map.put("password", mPWD.getText().toString());
                new MyUtils().xUtilsPost(URL.URL_USER_LOGIN, map, handler, URL.ID_USER_LOGIN);
            }
            break;

            case R.id.register_yanzheng_but: {
                Log.i("1", "onClick: 点击发送验证码");
                HashMap<String, String> map = new HashMap<>();
                map.put("phone", mPhone.getText().toString());
                map.put("send_type", "0");
                new MyUtils().xUtilsPost(URL.URL_SEND_CAPTCHA, map, handler, URL.ID_SEND_CAPTCHA);
            }
            break;
            case R.id.fanhui:{
                finish();
            }
            break;
            case R.id.zhuye: {

                startActivity(new Intent(UserRegisterActivity.this, MainActivity.class));
                finish();
            }
        }
    }
}
